package com.example.health_life.controller;

import com.example.health_life.Response;
import com.example.health_life.dao.Video;
import com.example.health_life.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class VideoController {
    
    @Autowired
    private VideoService videoService;

    @GetMapping("/videos")
    public Response<List<Video>> getVideoList(@RequestParam(required = false, defaultValue = "1") Integer status) {
        List<Video> videos = videoService.getVideoListWithAuthorByStatus(status);
        return Response.newSuccess(videos);
    }

    @GetMapping("/videos/course/{courseId}")
    public Response<List<Video>> getVideosByCourseId(@PathVariable Integer courseId) {
        List<Video> videos = videoService.getVideosByCourseIdWithAuthor(courseId);
        return Response.newSuccess(videos);
    }

    @GetMapping("/videos/{id}")
    public Response<Video> getVideoById(@PathVariable Integer id) {
        Video video = videoService.getVideoByIdWithAuthor(id);
        return Response.newSuccess(video);
    }

    @PostMapping("/videos")
    public Response<Integer> addVideo(@RequestBody Video video) {
        int id = videoService.addVideo(video);
        return Response.newSuccess(id);
    }

    @DeleteMapping("/videos/{id}")
    public Response<Integer> deleteVideo(@PathVariable Integer id) {
        int result = videoService.deleteVideo(id);
        return Response.newSuccess(result);
    }

    @PutMapping("/videos/{id}")
    public Response<Integer> updateVideo(@PathVariable Integer id,
                                       @RequestBody Video updateVideo) {
        int result = videoService.updateVideo(id, 
            updateVideo.getTitle(),
            updateVideo.getUrl(),
            updateVideo.getDuration(),
            updateVideo.getOrderNum(),
            updateVideo.getStatus(),
            updateVideo.getCoverImg());
        return Response.newSuccess(result);
    }
}
